package com.infore.statisticsAndAlarm.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.infore.statisticsAndAlarm.dto.WaterInstructionsDataDto;

/**
 * 水质等级计算工具
 * @author cys
 *
 */
public class WaterQualityUtil {
    
    public static final int invalidLevel = 0;
    
    // 测试
 	public static void main(String[] args) {
 		WaterInstructionsDataDto Instructions = new WaterInstructionsDataDto();
 		Instructions.setW21001("4");
 		Instructions.setW21003("4");
 		List<WaterInstructionsDataDto> InstructionsList = new ArrayList<WaterInstructionsDataDto>();
 		InstructionsList.add(Instructions);
 		WaterQualityUtil.setWaterQuality(InstructionsList);
 		System.out.println(Instructions.getWaterQuality());
 	}
    
    /** 水质等级 */
    private static final Map<String, Integer> LEVEL = new HashMap<String, Integer>() {
        private static final long serialVersionUID = 1L;
        {
            put("default", 0);
            put("one", 1);
            put("two", 2);
            put("three", 3);
            put("four", 4);
            put("five", 5);
            put("vfive", 6);
        }
    };
    
    /** 因子等级标准 */
    private static final Map<String, List<Double>> projectWaterQuality = new HashMap<String, List<Double>>() {
        private static final long serialVersionUID = 12L;

        {
            put("w01009", Arrays.asList(7.5, 6.0 , 5.0, 3.0, 2.0));//溶解氧 //大于
            put("w21003", Arrays.asList(0.15, 0.5, 1.0, 1.5, 2.0)); //氨氮
            put("w21011", Arrays.asList(0.02, 0.1 , 0.2, 0.3, 0.4)); //总磷
            put("w21001", Arrays.asList(0.2, 0.5 , 1.0, 1.5, 2.0));//总氮
            put("w01019", Arrays.asList(2.0, 4.0 , 6.0, 10.0, 15.0)); //高锰酸盐指数
            //重金属
//            put("w20141", Arrays.asList(0.05, 0.05,0.05, 0.1, 0.1));//砷
//            put("w20120", Arrays.asList(0.01, 0.01, 0.05, 0.05, 0.1)); //铅
//            put("w20121", Arrays.asList(0.02, 0.02 , 0.2, 0.3, 0.4)); //镍
//            put("w21001", Arrays.asList(0.2, 0.5 , 1.0, 1.5, 2.0));//总氮
//            put("w01019", Arrays.asList(2.0, 4.0 , 6.0, 10.0, 15.0)); //高锰酸盐指数
        }
    };

    public static void setWaterQuality(List<WaterInstructionsDataDto> finalList) {
        // TODO Auto-generated method stub
        String value = null;
        Integer level = null;
        Iterator<WaterInstructionsDataDto> it = finalList.iterator();
        while (it.hasNext()) {
            WaterInstructionsDataDto data = it.next();
            int maxLevel = 0;
            for (String code : projectWaterQuality.keySet()) {
                try {
                    Field field = data.getClass().getDeclaredField(code);
                 // 对象的属性的访问权限设置为可访问
                    field.setAccessible(true);
                    value = field.get(data) == null ? null : field.get(data).toString();
                    level = getWaterLevel(code, value);
                    if(level != null && level > maxLevel) {
                        maxLevel = level;
                    }
                } catch (NoSuchFieldException e) {
                    continue;
                } catch (SecurityException e) {
                    continue;
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            if(maxLevel == 0)
                maxLevel = invalidLevel;
            data.setWaterQuality(maxLevel+"");
        }
       
    }

    public static Integer getWaterLevel(String code, String value) {
        if(code == null || value == null)
            return null;
        List<Double> levelList = projectWaterQuality.get(code);
        double val = Double.valueOf(value);
        if (code.equals("w01009")){
            for (int i = 0; i < levelList.size(); i++) {
                if(val >= levelList.get(i)) {
                    return i+1;
                }
            }
        }else {
        	if (levelList == null || levelList.size() == 0) {
        		return 0;
			}
            for (int i = 0; i < levelList.size(); i++) {
                if(val <= levelList.get(i)) {
                    return i+1;
                }
            } 
        }
        return LEVEL.get("vfive");
        
    }
    
    /**计算单个断面综合指数**/
	public static String countCompositeIndex(WaterInstructionsDataDto data) {
    	Double value = 0.0;//总数计算结果值
    	Double projectvalue = 0.0;//因子值
    	int count = 0;//计数
    	String result = "";//结果
    	List<Double> valueArget = new ArrayList<Double>();//因子目标值
        for (String code : projectWaterQuality.keySet()) {
            try {
                Field field = data.getClass().getDeclaredField(code);
             // 对象的属性的访问权限设置为可访问
                field.setAccessible(true);
                projectvalue = field.get(data) == null ? null : Double.parseDouble(field.get(data).toString());//断面下每个因子的值
                if (projectvalue == null) {
                	continue;
				}
                valueArget = projectWaterQuality.get(code);//标准值集合
                value  = value + ( projectvalue / valueArget.get(2));
                count ++;
            } catch (NoSuchFieldException e) {
                continue;
            } catch (SecurityException e) {
                continue;
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if (count > 0) {
        	result  = String.format("%.3f", (value/count));
		}
    	return result;
    }
    
    
    
    
    
    
    
    
    
}
